package hos.base.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialog;

import hos.base.activity.BaseActivity;
import hos.base.view.IFragmentActivityX;
import hos.thread.IThread;

public abstract class BaseAppCompatDialog extends AppCompatDialog implements IThread, IFragmentActivityX<BaseActivity> {
    private View contentView;

    public BaseAppCompatDialog(@NonNull Context context) {
        this(context, 0);
    }

    public BaseAppCompatDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        LayoutInflater inflater = getLayoutInflater();
        contentView = onCreateView(inflater, null, null);
        setContentView(contentView);
        applyBackground(contentView);
        onViewShowed(contentView);
    }

    public View getContentView() {
        return contentView;
    }

    @NonNull
    public abstract View onCreateView(@NonNull LayoutInflater inflater,
                                      @Nullable ViewGroup container,
                                      @Nullable Bundle savedInstanceState);

    protected void applyBackground(View view) {
        int padding12 = DialogUtils.dp2px(12);
        int padding8 = DialogUtils.dp2px(8);
        view.setBackground(DialogUtils.getGradientDrawable());
        view.setPadding(padding12, padding8, padding12, padding8);
    }

    @Override
    public void show() {
        showDialog(Gravity.CENTER);
    }

    @Override
    public void dismiss() {
        postOnMain(() -> {
            if (isShowing()) {
                BaseAppCompatDialog.super.dismiss();
            }
        });
    }

    public void showDialog(int gravity) {
        postOnMain(() -> {
            if (!isShowing()) {
                BaseAppCompatDialog.super.show();
                Window currentWindow = getWindow();
                if (currentWindow == null) {
                    return;
                }
                currentWindow.setBackgroundDrawableResource(android.R.color.transparent);
                ViewGroup group = (ViewGroup) currentWindow.getDecorView();
                ViewGroup child = (ViewGroup) group.getChildAt(0);
                child.post(() -> {
                    WindowManager.LayoutParams param = currentWindow.getAttributes();
                    if (gravity == Gravity.BOTTOM) {
                        if (contentView != null) {
                            contentView.setBackground(DialogUtils.getGradientDrawableBottom());
                        }
                        param.width = WindowManager.LayoutParams.MATCH_PARENT;
                    } else {
                        param.width = (int) (0.8 * DialogUtils.getWindowWidth(currentWindow));
                    }
                    param.gravity = gravity;
                    currentWindow.setGravity(gravity);
                    currentWindow.setAttributes(param);
                });
            }
        });
    }


}